package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MainCardListBean;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ThrottleFirstUtil;
import com.zzsy.carosshopprojects.view.dialog.GoUpPopupDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 补卡功能的 第二个页面 进行会员信息展示 并重新绑定一张实体卡
 */
public class CardReplaceMemberFragment extends BaseFragment {

    public static CardReplaceMemberFragment newInstance(MainCardListBean.MyMainCardsBean bean) {
        CardReplaceMemberFragment cardReplaceMemberFragment = new CardReplaceMemberFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        cardReplaceMemberFragment.setArguments(bundle);
        return cardReplaceMemberFragment;
    }

    @BindView(R.id.tv_name)
    TextView etMemberName;
    @BindView(R.id.tv_card_company)
    TextView tvCardCompany;
    @BindView(R.id.tv_member_card_type)
    TextView tvMemberCardType;
    @BindView(R.id.tv_member_num)
    TextView tvMemberNum;
    @BindView(R.id.tv_member_banbal)
    TextView tvMemberBanace;
    @BindView(R.id.tv_member_phone)
    TextView tvMemberPhone;
    @BindView(R.id.tv_member_sure)
    TextView tvMmeberSure;
    private MainCardListBean.MyMainCardsBean bean;
    private String id;

    @Override
    protected int initLayout() {
        return R.layout.fg_cardreplacemember;
    }

    @Override
    protected void initView(View view) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initTitle(view, getResources().getString(R.string.MemberMessage), getActivity());
    }

    @Override
    protected void initData(Context mContext) {
        bean = (MainCardListBean.MyMainCardsBean) getArguments().getSerializable("bean");
        id = bean.getId() == null ? "" : bean.getId();
        setData();
    }

    @Override
    protected void loadMethod() {

    }

    @OnClick({R.id.tv_member_sure})
    public void onViewClicked(View view) {
        if (ThrottleFirstUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_member_sure:
                GoUpPopupDialog.newInstance(0, false, GoUpPopupDialog.REPLACEMENTCARD).show(getFragmentManager(), "dialog");
                break;

        }
    }

    private void setData() {
        String productName = bean.getProductName();
        String memberNum = bean.getMemberNum();
        String phone = bean.getPhone();
        String realName = bean.getRealName();
        String rechargeBalance = bean.getRechargeBalance();
        String cardKindName = bean.getCardKindName();
        //卡类型
        if (!StringUtils.isEmpty(productName)) {
            tvMemberCardType.setText(productName);
        }

        if (!StringUtils.isEmpty(memberNum)) {
            tvMemberNum.setText(memberNum);
        }

        //账户余额
        if (!StringUtils.isEmpty(rechargeBalance)) {
            String balance = ConverterHelper.toBigDecimal(rechargeBalance).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            tvMemberBanace.setText("¥"+balance);
        }

        if (!StringUtils.isEmpty(phone)) {
            tvMemberPhone.setText(phone);
        }


        if (!StringUtils.isEmpty(realName)) {
            etMemberName.setVisibility(View.VISIBLE);
            etMemberName.setText(realName);
        } else {
            etMemberName.setVisibility(View.GONE);
        }

        if (!StringUtils.isEmpty(cardKindName)) {
            tvCardCompany.setText(cardKindName);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.message) {
            case MessageEvent.REPLACEMENTCARD:
                deleteUUid(event.content);
                break;
        }
    }

    private void deleteUUid(String uuid) {

        RequestUtils.UpdateCardByUuid(getContext(), id, uuid, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {
                startWithPop(CarReplaceSufFragment.newInstance());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    startWithPop(CarReplaceSufFragment.newInstance());
                } else {
                    ToastUtils.showToast(getContext().getApplicationContext(), errorMsg);
                }
            }
        });
    }
}
